﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO.JobGradeManagementDTO;
using ThridGroup.ERP.DTO.ProductionSystemDto.ProductionMaterialReturnDto;
using ThridGroup.ERP.ProductionSystem;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace ThridGroup.ERP.ProductionSystem
{
    /// <summary>
    /// 生产退料Services
    /// </summary>
    public class ProductionMaterialReturnServices : ApplicationService, IProductionMaterialReturnServices
    {
        /// <summary>
        /// 退料仓储
        /// </summary>
        private readonly IRepository<ProductionMaterialReturn, int> _productionMaterialReturnServices;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productionMaterialReturnServices">生产提料</param>
        public ProductionMaterialReturnServices(IRepository<ProductionMaterialReturn, int> productionMaterialReturnServices)
        {
            _productionMaterialReturnServices = productionMaterialReturnServices;
        }

        /// <summary>
        /// 添加生产退料信息
        /// </summary>
        /// <returns></returns>
        public async Task<int> CreateProductionMaterialReturn(CreateProductionMaterialReturnDto createProductionMaterialReturnDto)
        {
            var materialReturn = ObjectMapper.Map<CreateProductionMaterialReturnDto, ProductionMaterialReturn>(createProductionMaterialReturnDto);
            var result = await _productionMaterialReturnServices.InsertAsync(materialReturn);
            return result == null ? 0 : 1;
        }

        /// <summary>
        /// 显示生产退料信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<CreateProductionMaterialReturnDto>> QueryProductionMaterialReturn(GetProductionMaterialReturnDto getProductionMaterialReturnDto)
        {
            var materialReturnList = await _productionMaterialReturnServices.GetListAsync();
            var filteredData = materialReturnList.AsQueryable();
            //按退料编号查询
            filteredData = filteredData.WhereIf(!string.IsNullOrEmpty(getProductionMaterialReturnDto.ReturnOrderCode),x => x.ReturnOrderCode.Contains(getProductionMaterialReturnDto.ReturnOrderCode));
            //按生产工单查询
            filteredData = filteredData.WhereIf(!string.IsNullOrEmpty(getProductionMaterialReturnDto.ProductionWorkOrder),x => x.ProductionWorkOrder.Contains(getProductionMaterialReturnDto.ProductionWorkOrder));
            //按退料仓库查询
            filteredData = filteredData.WhereIf(!string.IsNullOrEmpty(getProductionMaterialReturnDto.ReceiveWarehouse),x => x.ReceiveWarehouse.Contains(getProductionMaterialReturnDto.ReceiveWarehouse));
            var result = filteredData.Select(x => ObjectMapper.Map<ProductionMaterialReturn, CreateProductionMaterialReturnDto>(x)).ToList();
            return result;
        }

        /// <summary>
        /// 根据ID获取生产退料单详情
        /// </summary>
        public async Task<CreateProductionMaterialReturnDto> GetProductionMaterialReturnById(int id)
        {
            // 获取实体
            var entity = await _productionMaterialReturnServices.FindAsync(id);
            if (entity == null)
            {
                return null;
            }
            return ObjectMapper.Map<ProductionMaterialReturn, CreateProductionMaterialReturnDto>(entity);
           
        }

        /// <summary>
        /// 修改退料状态
        /// </summary>
        public async Task<int> UpProductionMaterialReturnById(int id)
        {
            // 获取实体
            var entity = await _productionMaterialReturnServices.FindAsync(id);

           entity.Operation = "已退料";
            var result = await _productionMaterialReturnServices.UpdateAsync(entity);
            return result == null ? 0 : 1;
        }

        /// <summary>
        /// 修改生产退料信息
        /// </summary>
        /// <param name="dto">生产退料DTO</param>
        /// <returns>1成功，0失败</returns>
        public async Task<int> UpdateProductionMaterialReturn(UpdateProductionMaterialReturnDto dto)
        {
            var materialReturn = ObjectMapper.Map<UpdateProductionMaterialReturnDto, ProductionMaterialReturn>(dto);
            var result = await _productionMaterialReturnServices.UpdateAsync(materialReturn);
            return result == null ? 0 : 1;
        }

        /// <summary>
        /// 批量删除生产退料信息
        /// </summary>
        /// <param name="ids">要删除的生产退料ID列表</param>
        public async Task BatchDeleteProductionMaterialReturn(List<int> ids)
        {
            if (ids == null || !ids.Any())
                return;
            await _productionMaterialReturnServices.DeleteDirectAsync(a => ids.Contains(a.Id));
        }
    }
}
